package com.wawayaya.api.remote;

import android.content.Context;
import java.util.Date;
import com.wawayaya.api.ApiRequestHelper;
import com.wawayaya.api.ApiRequestParams;
import com.wawayaya.api.interf.ApiResponseJsonListener;
import com.wawayaya.bean.User;
import com.wawayaya.cache.UserDataCache;
import com.wawayaya.util.TLog;
import org.json.JSONObject;

/**
 * Created by creative on 15/11/9.
 */
public class ApiWawayaya {

    /**
     *  验证请求时间是否在5分钟之内
     * @param timestamp
     */
    private  static void  calculateTime(String timestamp){
        long currentTime = new Date().getTime();
        long  almost= currentTime-new Long(timestamp);
        long  fiveSecond =5*60*1000;
        if(almost<fiveSecond){
            return ;
        }
    }

    /**
     * 书架网络请求
     * @param context
     * @param requestType
     * @param params
     * @param apiResponseJsonListener
     */
    public static  void  shelfRequest(Context context,int  requestType,ApiRequestParams params,
                                             ApiResponseJsonListener apiResponseJsonListener){
        if(null==params){
            params = new ApiRequestParams();
        }
        /**每次取得最新的时间戳*/
        switch (requestType){
            case ApiConstans.LOGINJOYREADER:
               // params.put("data_timestamp",!"".equals(timestamp.getUsers())?timestamp.getUsers():"0");
                params.put("data_timestamp","0");
                ApiRequestHelper.postHandler(ApiConstans.getLoginJoyreader(), context, params, ApiConstans.LOGINJOYREADER, apiResponseJsonListener);
            break;

            case ApiConstans.CONFIG:
                params.put("data_timestamp","0");
                ApiRequestHelper.postHandler(ApiConstans.getConfigUrl(), context, params, ApiConstans.CONFIG, apiResponseJsonListener);
            break;

            case ApiConstans.BOOKSHELF:
                params.put("data_timestamp","0");
                ApiRequestHelper.postHandler(ApiConstans.getBookSelfListUrl(), context, params, ApiConstans.BOOKSHELF, apiResponseJsonListener);
                break;

            case ApiConstans.BOOK:
                params.put("data_timestamp","0");
                ApiRequestHelper.postHandler(ApiConstans.getShelfs(), context, params, ApiConstans.BOOK, apiResponseJsonListener);
                break;

            case ApiConstans.ADVERT:
                params.put("data_timestamp","0");
                ApiRequestHelper.postHandler(ApiConstans.getAdvert(), context, params, ApiConstans.ADVERT, apiResponseJsonListener);
                break;

            case ApiConstans.HOMEPAGE:
                params.put("data_timestamp","0");
                ApiRequestHelper.postHandler(ApiConstans.getHomepage(), context, params, ApiConstans.HOMEPAGE, apiResponseJsonListener);
                break;

            case ApiConstans.BANNER:
                params.put("data_timestamp","0");
                ApiRequestHelper.postHandler(ApiConstans.getBannerUrl(), context, params, ApiConstans.BANNER, apiResponseJsonListener);
            break;

            case ApiConstans.AREA:
                params.put("data_timestamp","0");
                ApiRequestHelper.postHandler(ApiConstans.getCountryUrl(), context, params, ApiConstans.AREA, apiResponseJsonListener);
            break;


            case ApiConstans.SUPPORT:
                params.put("data_timestamp","0");
                ApiRequestHelper.postHandler(ApiConstans.getSupportUrl(), context, params, ApiConstans.SUPPORT, apiResponseJsonListener);
            break;

            case ApiConstans.MYBOOKSHELF:
                params.put("data_timestamp","0");
                ApiRequestHelper.postHandler(ApiConstans.getMyBookShelf(), context, params, ApiConstans.MYBOOKSHELF, apiResponseJsonListener);
            break;

            case ApiConstans.MYBOOKSHELF_MERGEMINE:
                params.put("data_timestamp","0");
                ApiRequestHelper.post(ApiConstans.getMergeMine(), context, params, ApiConstans.MYBOOKSHELF_MERGEMINE, apiResponseJsonListener);
            break;

            case ApiConstans.NOTICE:
                params.put("data_timestamp","0");
                ApiRequestHelper.postHandler(ApiConstans.getNotice(), context, params, ApiConstans.NOTICE, apiResponseJsonListener);
            break;
        };
    }

    /**
     * 图书阅读 - 在线取词
     * @param context
     * @param params
     * @param apiResponseJsonListener
     */
    public static  void  getWordDict(Context context,ApiRequestParams params,
                                     ApiResponseJsonListener apiResponseJsonListener){
        ApiRequestHelper.postHandler(ApiConstans.getDictInfo(), context, params, ApiConstans.WORD, apiResponseJsonListener);
    }

    /**
     *   从未登陆 过joyreader 用一账通信息进行 登陆
     * @param jsonObject
     */
    public static void userLogin(Context context,JSONObject jsonObject, ApiResponseJsonListener apiResponseJsonListener) {
        TLog.log("account joylogin");
        ApiRequestParams params = new ApiRequestParams();
        params.put("userId", jsonObject.optLong("uid"));
        params.put("caToken", jsonObject.optString("token"));

        ApiRequestHelper.postHandler(ApiConstans.getLoginUrl(), context, params, ApiConstans.USER_LOGIN, apiResponseJsonListener);
    }

    /**
     *   已经通过一账通登陆 过，通过 loginJoyReader 进行数据刷新
     * @param
     */
    public static void loginJoyReader(Context context, ApiResponseJsonListener apiResponseJsonListener) {
        TLog.log("loginJoylogin joyreader on shef");
        ApiRequestParams params = new ApiRequestParams();

        User user = UserDataCache.getInstance().getUserinfor();
        params.put("userId", user.getUserId());
        params.put("token", user.getToken());

        ApiRequestHelper.postHandler(ApiConstans.getLoginJoyreader(), context, params, ApiConstans.LOGINJOYREADER, apiResponseJsonListener);
    }



}
